﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using Gift.Model.ENUM;
using Gift.DAL;
using Gift.Model;
using System.Data;

namespace WebSite.admin.admin
{
    public partial class adminList : BaseAdmin
    {
        readonly DALAdminLogin theAdminLogin = new DALAdminLogin();
        tbAdminlogin oAdminLogin;
        readonly MemberAccount theMemberAccount = new MemberAccount();
        // Methods
        private void DeletedAdmin()
        {

            if ((base.Request.QueryString["uid"] != null) && (base.Request.QueryString["action"] != null))
            {
                oAdminLogin = theAdminLogin.GetModel(int.Parse(base.Request.QueryString["uid"].ToString()));
                oAdminLogin.IsDeleted = true;
                theAdminLogin.Update(oAdminLogin);
                ExtAspNet.Alert.Show("删除管理员成功！");
                this.BindGrid();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.AdminList);
                this.DeletedAdmin();
                this.BindGrid();
            }
        }


        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
        }

        string GetSqlWhere
        {
            get
            {
                StringBuilder strWhere = new StringBuilder();
             
                if (this.ddlAdminType.SelectedItem.Value == "0")
                {
                    strWhere.Append( " and isnull(a.AdminTypeId,0) = 0 and  a.AdminId not  in (select AdminId from tbCompanyAdmin)");
                }
                else if (this.ddlAdminType.SelectedItem.Value == "1")
                {
                    //strWhere.Append(" and isnull(a.AdminTypeId,0) = 1  and isnull(ca.AdminTypeId,0) = 1 ");
                    strWhere.Append(" and isnull(a.AdminTypeId,0) = 0  and a.AdminId in (select AdminId from tbCompanyAdmin)");
                }
                else if (this.ddlAdminType.SelectedItem.Value == "2")//业务员
                {
                    strWhere.Append(" and isnull(a.AdminTypeId,0) = 1  and isnull(ca.AdminTypeId,0) = 1 ");
                   // strWhere.Append(" and isnull(a.AdminTypeId,0) = 1 and isnull(ca.AdminTypeId,0) = 1  ");
                }
                return strWhere.ToString();
            }
        }


        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table =  theAdminLogin.GetAdminList(GetSqlWhere).Tables[0];
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            Grid1.DataSource = view1;
            Grid1.DataBind();
        }
        private void BindGrid()
        {
            DataTable table = theAdminLogin.GetAdminList(GetSqlWhere).Tables[0];

                Grid1.DataSource = table;
                Grid1.DataBind();
    

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }

    }
}


//using System;
//using System.Collections.Generic;

//using System.Web;
//using System.Web.UI;
//using System.Web.UI.WebControls;
//using System.Data;
//using System.Web.Profile;

//namespace WebSite.admin.admin
//{
//    public partial class userlist : BaseAdmin
//    {


//        // Methods
//        private void bind()
//        {
//            if (Request.QueryString["action"] == "del")
//            {
//                string sSql = "update tbAdminLogin set IsDeleted=1 where Adminid = " + Request.QueryString["uid"];
//                SQLAccess.DoSql(sSql);
//                base.Response.Write("<script language='javascript'>;alert('删除成功');window.location.href='userlist.aspx';</script>");
//                base.Response.End();
//            }

//            string sql = "select a.*,isnull((select [name] from tbCompany  where id = ca.CompanyId),a.AdminName) as CompanyName,ca.CompanyId "
//                       +" from tbAdminLogin a left join tbCompanyAdmin ca on a.AdminId = ca.AdminId  "
//                        + " where 1=1 and isnull(IsDeleted,0) = 0  ";

          

//            sql = sql + " order by Companyname ";
//            DataTable dtSource = null;
//            DataSet ds = SQLAccess.GreatDs(sql);
//            if (ds != null && ds.Tables.Count > 0)
//            {
//                dtSource = ds.Tables[0];
//            }

//            dtSource.DefaultView.RowFilter = string.Format(" AdminUser like '%{0}%' or CompanyName like '%{0}%'", this.tbxKeywords.Text.Trim());
//            this.GridView1.DataSource = dtSource.DefaultView;
//            this.GridView1.DataBind();

//            this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
//            this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
//            this.LblRecordCount.Text = "总计 " + dtSource == null ? "0" : dtSource.DefaultView.Count.ToString() + "个记录";
//            if (dtSource == null || dtSource.DefaultView.Count == 0)
//            {
//                this.btnFirst.Visible = false;
//                this.btnPrev.Visible = false;
//                this.btnNext.Visible = false;
//                this.btnLast.Visible = false;
//                this.LblCurrentIndex.Visible = false;
//                this.LblPageCount.Visible = false;
//                this.LblRecordCount.Visible = false;
//            }
//            else if (this.GridView1.PageCount == 1)
//            {
//                this.btnFirst.Visible = false;
//                this.btnPrev.Visible = false;
//                this.btnNext.Visible = false;
//                this.btnLast.Visible = false;
//            }
//            this.btnFirst.CommandName = "1";
//            this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
//            this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
//            this.btnLast.CommandName = this.GridView1.PageCount.ToString();
//            this.ddlCurrentPage.Items.Clear();
//            if (this.GridView1.PageCount > 0)
//            {
//                for (int i = 1; i <= this.GridView1.PageCount; i++)
//                {
//                    this.ddlCurrentPage.Items.Add(i.ToString());
//                }
//                this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
//            }
//        }

//        protected void PagerButtonClick(object sender, EventArgs e)
//        {
//            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
//            this.bind();
//        }
//        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
//        {
//            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
//            this.bind();
//        }





//        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
//        {
//            this.GridView1.PageIndex = e.NewPageIndex;
//            this.bind();
//        }

//        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
//        {
//            if (e.Row.RowType == DataControlRowType.DataRow)
//            {

//                //当鼠标停留时更改背景色
//                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#efefef'");
//                //当鼠标移开时还原背景色
//                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

//            }
//        }

//        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
//        {
//        }

//        protected void Page_Load(object sender, EventArgs e)
//        {

//            if (!base.IsPostBack)
//            {
//                base.ValidateLoginRule(Gift.Model.ENUM.ERule.AdminList);
       
//                 this.bind();
                  
//            }
//        }

//        protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
//        {
//            this.bind();
//        }


//    }
//}